package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.answer.entity.AnswerEntity;
import com.forlove.app.qselect.friend.service.IAddFriendGuideService;
import com.forlove.app.qselect.friend.service.impl.AddFriendGuideServiceImpl;
import com.forlove.app.qselect.friend.view.AddFriendGuideView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/12/11.
 */

public class AddFriendGuidePresenter extends IBasePresenter<IAddFriendGuideService> {
    private IAddFriendGuideService addFriendGuideService;
    private AddFriendGuideView addFriendGuideView;

    public AddFriendGuidePresenter(AddFriendGuideView addFriendGuideView) {
        this.addFriendGuideView = addFriendGuideView;
        if (addFriendGuideService == null) {
            addFriendGuideService = new AddFriendGuideServiceImpl();
            setService(addFriendGuideService);
        }
    }


    public synchronized void loadQuestionData() {
        Map<String, String> param = new HashMap<>();
        addFriendGuideService.loadData(ServerManager.ACTION_GET_SUBJECT, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "获取题目 ：" + result);
                AnswerEntity answerEntity = ParseHelper.getModel(result, "data", AnswerEntity.class);
                addFriendGuideView.updateSubjectsView(answerEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "获取题目 onFail" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    addFriendGuideView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else if (addFriendGuideView != null) {
                    addFriendGuideView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "请稍后重试!");
                }
            }
        });
    }

}
